﻿using WebApp.BLL;
using WebApp.Web.Core.Auth;
using WebApp.BLL.Basic;
using WebApp.BLL.Manage;
using WebApp.Common;
using WebApp.Models;
using WebApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Web.Core;

namespace kzjd.rycp.WebApp.Areas.Basic.Controllers
{
    public class DocumentController : Controller
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger("WebApp.Log");

        #region DocumentJson

        /// <summary>
        /// 查询列表数据
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="s"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [AuthorizeFilter("Basic.Document")]
        public JsonResult DocumentJson(int page, int rows, string s, string sort, string order)
        {
            S_documentService docservice = new S_documentService();
            S_userService user = new S_userService();
            S_dictService dict = new S_dictService();
            Dictionary<string, object> result = new Dictionary<string, object>();

            int rowscount = 0;

            var list = docservice.PageList(page, rows, s.Trim(), sort, order, out rowscount).Select(p => new
            {
                p.Id,
                p.Title,
                p.Category,
                p.DocKey,
                Updatetime = CommonStr.GetDateTimeStr(p.Updatetime),
                Updateuser = user.GetUserRealname(p.Updateuser)
            });

            result.Add("total", rowscount);
            result.Add("rows", list);
            return Json(result);
        }

        #endregion

        #region List

        [AuthorizeFilter("Basic.Document")]
        // GET: Basic/Radiotherapy
        public ActionResult List()
        {
            //编辑权限
            ViewBag.CanEdit = true;// CommonStr.HasAuth("Basic.Document.Edit");

            return View();
        }
        #endregion

        #region Edit

        [AuthorizeFilter("Basic.Document")]
        public ActionResult Edit(int id)
        {
            S_documentService docservice = new S_documentService();

            //是否有编辑权限
            ViewBag.CanEdit = true; // CommonStr.HasAuth("Basic.Company.Edit");

            if (id == 0)
            {
                return View(new S_document
                {
                    Content = "",
                    ContentHK = ""
                });
            }
            else
            {
                S_document item = docservice.Find(id);

                if (item == null)
                {
                    throw new HttpException(404, "Document(id='" + id.ToString() + "') not found!");
                }

                return View(item);
            }
        }

        #endregion

        #region 删除

        [AuthorizeFilter("Basic.Document")]
        public JsonResult Delete(string ids)
        {
            try
            {
                new S_documentService().DeleteByIds(ids);

                return Json(new AjaxResult("0", CSMSLang.Get("message_delete_success")));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }
        }

        #endregion

        #region 重复验证
        [IgnoreAuthorizeFilter]
        public string ExistCheck(int id, string dockey)
        {
            return new S_documentService().Exist(id, dockey) ? "no" : "yes";
        }
        #endregion

        #region 保存
        //保存
        [AuthorizeFilter("Basic.Document")]
        [ValidateInput(false)]
        public JsonResult Save(string json)
        {
            try
            {
                S_documentService docservice = new S_documentService();

                JsonDataModel model = Newtonsoft.Json.JsonConvert.DeserializeObject<JsonDataModel>(json);

                S_document item = new S_document();

                if (model.Id > 0)
                {
                    item = docservice.Find(model.Id);
                }
                model.SetModelData(item, true);
                docservice.Save(item);
                return Json(new AjaxResult(AjaxResult.RESULT_OK, CSMSLang.Get("message_save_success")));
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return Json(new AjaxResult(AjaxResult.RESULT_ERROR, ex.Message));
            }

        }
        #endregion
    }
}